'use strict';

goog.provide('GBlockly.FieldTextInput');
goog.require('Blockly.Blocks');
goog.require('goog.ui.Dialog');
goog.require('goog.ui.Slider');
GBlockly.FieldTextInput = function(text) {
	GBlockly.FieldTextInput.superClass_.constructor.call(this, text);
};
goog.inherits(GBlockly.FieldTextInput, Blockly.FieldTextInput);

GBlockly.FieldTextInput.prototype.showEditor_ = function(opt_quietInput) {
	return;
}

GBlockly.FieldTextInput.prototype.setValue = function(text) {
	if (text === null) {
    return;
}
if (this.sourceBlock_) {
	var validated = this.callValidator(text);
    if (validated !== null) {
    	text = validated;
    }
}
	Blockly.FieldTextInput.prototype.setValue.call(this, text);
};

var numdian_string = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10'];


//program
Blockly.Blocks['gao_program'] = {
	init: function() {
		this.appendDummyInput()
			.appendField("Program");
		this.appendStatementInput("gaotext")
			.setCheck(null);
		this.setPreviousStatement(true, null);
		this.setNextStatement(true, null);
		this.setColour(20);
		this.setTooltip('');
		this.setHelpUrl('');
	}
};
Blockly.JavaScript['gao_program'] = function(a) {
	var b = Blockly.JavaScript.statementToCode(a, 'gaotext');
	var code = 'Program\n' + b + 'End Program\n';
	return code;
};


//while
Blockly.Blocks['gao_while'] = {
	init: function() {
		this.appendDummyInput()
			.appendField("while")
			.appendField(new GBlockly.FieldTextInput("1"), "whilenum");
		this.appendStatementInput("whinput")
			.setCheck(null);
		this.setPreviousStatement(true, null);
		this.setNextStatement(true, null);
		this.setColour(20);
		this.setTooltip('');
		this.setHelpUrl('');
	},
	onchange: function(ev) {
		if(ev.type == Blockly.Events.UI && ev.element == 'click' && this.id == ev.blockId) {
			var block = this;
			var dialog = new goog.ui.Dialog();
			dialog.setContent(
				'<div id="futitle" style="position: absolute;top: 12%;left:0%;display: none;">变量</div>'+
				
				'<div style="position:absolute;left: 45%;top: 33%;color:#fff;">While <span id="var" style="color:#00FFFF;">1</span></div>'+
				'<input id="var_val" type="range" min="1" style="position:absolute;left: 22%;top: 41%;z-index:1000;"/>'+
				
				'<div id="cancelsruebut"><span id="cancelbut">取消</span><span id="sruebut">确定</span></div>'
			);
			dialog.setHasTitleCloseButton(false);
			dialog.setDraggable(false);
			dialog.setVisible(true);
			dialog.setDisposeOnHide(true);
			//保存当前的点位和速度的值
			var a = block.getFieldValue('whilenum');
			
			//设置变量
			var ccn;
			ccn=block.getFieldValue('whilenum');
			$("#var_val").css("background-size",ccn+"%"+" "+100+"%");
			$("#var").html(ccn);
			$('#var_val')[0].value=ccn;
			$('#var_val').on('input propertychange',function(){
			 	ccn=$('#var_val').val();
		        $("#var_val").css("background-size",ccn+"%"+" "+100+"%");
		        $("#var").html(ccn);
		        block.setFieldValue(ccn, 'whilenum');
		    });
			
			//取消设定的值，还原到原来的值
			$("#cancelbut").on('click',function(){
				block.setFieldValue(a, 'whilenum');
				$(".modal-dialog-bg").remove();
				$(".modal-dialog").remove();
			});
			//确定
			$("#sruebut").on('click',function(){
				$(".modal-dialog-bg").remove();
				$(".modal-dialog").remove();
			});
		}
	}
};
Blockly.JavaScript['gao_while'] = function(a) {
	var n = a.getFieldValue('whilenum');
	var b = Blockly.JavaScript.statementToCode(a, 'whinput');
	var code = 'while ' + n + '\n' + b + 'end while\n';
	return code;
};


//gao_move
Blockly.Blocks['gao_move'] = {
	init: function() {
		this.appendDummyInput()
			.appendField("Move")
			.appendField(new GBlockly.FieldTextInput("PUMA"), "move1")
			.appendField("JP")
			.appendField(new GBlockly.FieldTextInput("1"), "move2")
			.appendField("速度")
			.appendField(new GBlockly.FieldTextInput("30"), "move3");
		this.setPreviousStatement(true, null);
		this.setNextStatement(true, null);
		this.setColour(230);
		this.setTooltip('');
		this.setHelpUrl('');
	},
	onchange: function(ev) {
		if(ev.type == Blockly.Events.UI && ev.element == 'click' && this.id == ev.blockId) {
			var block = this;
			var dialog = new goog.ui.Dialog();
			dialog.setContent(
				'<div id="futitle" style="position: absolute;top: 20%;left:0%;display: none;">移动到点,速度</div>'+
				
				'<div style="position:absolute;left: 47%;top: 22%;color:#fff;">点位 <span id="dian" style="color:#00FFFF;">1</span></div>'+
				'<div id ="numdian" style="display:block; width: 70%; height: 70%; position: absolute; left: 24%; top: 12%; background: transparent;">' +
				'<div style="font-size: 32px;position: absolute; left: 0%; top: 25%; width: 15%; height: 15%;text-align: center;">1</div>' +
				'<div style="font-size: 32px;position: absolute; left: 15%; top: 25%; width: 15%; height: 15%;text-align: center;">2</div>' +
				'<div style="font-size: 32px;position: absolute; left: 30%; top: 25%; width: 15%; height: 15%;text-align: center;">3</div>' +
				'<div style="font-size: 32px;position: absolute; left: 45%; top: 25%; width: 15%; height: 15%;text-align: center;">4</div>' +
				'<div style="font-size: 32px;position: absolute; left: 60%; top: 25%; width: 15%; height: 15%;text-align: center;">5</div>' +
				'<div style="font-size: 32px;position: absolute; left: 0%; top: 40%; width: 15%; height: 15%;text-align: center;">6</div>' +
				'<div style="font-size: 32px;position: absolute; left: 15%; top: 40%; width: 15%; height: 15%;text-align: center;">7</div>' +
				'<div style="font-size: 32px;position: absolute; left: 30%; top: 40%; width: 15%; height: 15%;text-align: center;">8</div>' +
				'<div style="font-size: 32px;position: absolute; left: 45%; top: 40%; width: 15%; height: 15%;text-align: center;">9</div>' +
				'<div style="font-size: 32px;position: absolute; left: 60%; top: 40%; width: 15%; height: 15%;text-align: center;">10</div>' +
				'</div>' +
				
				'<div style="position:absolute;left: 47%;top: 55%;color:#fff;">速度<span id="speed2" style="color:#00FFFF;">100</span><span style="color:#00FFFF;">%</span></div>'+
				'<input id="move_speed" type="range" style="position:absolute;left: 22%;top: 62%;"/>'+
				
				'<div id="cancelsruebut"><span id="cancelbut">取消</span><span id="sruebut">确定</span></div>'
				
			);
			dialog.setHasTitleCloseButton(false);
			dialog.setDraggable(false);
			dialog.setVisible(true);
			dialog.setDisposeOnHide(true);
			
			//保存当前的点位和速度的值
			var a = block.getFieldValue('move2');
			var b = block.getFieldValue('move3');
			//设置点位
			var numdian = document.getElementById('numdian').getElementsByTagName('div');
			var dian = document.getElementById('dian');
			dian.innerHTML = block.getFieldValue('move2');
			$('#numdian div').eq(dian.innerHTML-1).css('color','#00C5AC');
			for(var i = 0; i < numdian.length; i++) {
				numdian[i].index = i;
				numdian[i].onclick=function() {
					block.setFieldValue(numdian_string[this.index], 'move2');
					dian.innerHTML = block.getFieldValue('move2');
					$('#numdian div').eq(this.index).css('color','#00C5AC').siblings().css('color','#fff');
				}
			}
			//设置速度
			var con;
			con=block.getFieldValue('move3');
			$("#move_speed").css("background-size",con+"%"+" "+100+"%");
			$("#speed").html(con);
			$("#speed2").html(con);
			$('#move_speed')[0].value=con;
			$('#move_speed').on('input propertychange',function(){
			 	con=$('#move_speed').val();
		        $("#move_speed").css("background-size",con+"%"+" "+100+"%");
		        $("#speed").html(con);
		        $("#speed2").html(con);
		        block.setFieldValue(con, 'move3');
		    });
			//取消设定的值，还原到原来的值
			$("#cancelbut").on('click',function(){
				block.setFieldValue(a, 'move2');
				block.setFieldValue(b, 'move3');
				$(".modal-dialog-bg").remove();
				$(".modal-dialog").remove();
			});
			//确定
			$("#sruebut").on('click',function(){
				$(".modal-dialog-bg").remove();
				$(".modal-dialog").remove();
			});
		}
	}
};
Blockly.JavaScript['gao_move'] = function(block) {
  var b = block.getFieldValue('move1');
  var d = block.getFieldValue('move2');
  var e = block.getFieldValue('move3');
  var code = 'Move ' + b + ' JP ' + d + ' Vcruise=' + e + '\n';
  return code;
};


//for语句
Blockly.Blocks['gao_for'] = {
	init: function() {
		this.appendDummyInput()
			.appendField("for")
			.appendField(new GBlockly.FieldTextInput("i"), "bian");
		this.appendValueInput("form")
			.setCheck(null)
			.appendField("从");
		this.appendValueInput("to")
			.setCheck(null)
			.appendField("到");
		this.appendStatementInput("do")
			.setCheck(null);
		this.setInputsInline(true);
		this.setPreviousStatement(true, null);
		this.setNextStatement(true, null);
		this.setColour(180);
		this.setTooltip('');
		this.setHelpUrl('');
	}
};
Blockly.JavaScript['gao_for'] = function(a) {
	var b = a.getFieldValue('bian');
	var f = Blockly.JavaScript.valueToCode(a, 'form', Blockly.JavaScript.ORDER_ATOMIC);
	var t = Blockly.JavaScript.valueToCode(a, 'to', Blockly.JavaScript.ORDER_ATOMIC);
	var c = Blockly.JavaScript.statementToCode(a, 'do'),
		c = Blockly.JavaScript.addLoopTrap(c, a.id),
		v = "for " + b + " = " + f + " to " + t + "\n" + c + "next\n";
	return v;
};


//if语句
Blockly.Blocks['gao_if'] = {
	init: function() {
		this.appendDummyInput()
			.appendField("if")
			.appendField(new GBlockly.FieldTextInput("1"), "bian")
			.appendField("then");
		this.appendStatementInput("ifdo")
			.setCheck(null)
			.appendField("do");
		this.appendStatementInput("elsedo")
			.setCheck(null)
			.appendField("else");
		this.setInputsInline(true);
		this.setPreviousStatement(true, null);
		this.setNextStatement(true, null);
		this.setColour(180);
		this.setTooltip('');
		this.setHelpUrl('');
	}
};
Blockly.JavaScript['gao_if'] = function(a) {
	var b = a.getFieldValue('bian');
	var f = Blockly.JavaScript.statementToCode(a, 'ifdo'),
		f = Blockly.JavaScript.addLoopTrap(f, a.id);
	var e = Blockly.JavaScript.statementToCode(a, 'elsedo'),
		e = Blockly.JavaScript.addLoopTrap(e, a.id);
	var c = "if " + b + " then\n" + f + "else\n" + e + "end if\n";
	return c;
};


//gao_sleep
Blockly.Blocks['gao_sleep'] = {
	init: function() {
		this.appendDummyInput()
			.appendField("sleep")
			.appendField(new GBlockly.FieldTextInput("1"), "wait1");
		this.setPreviousStatement(true, null);
		this.setNextStatement(true, null);
		this.setColour(180);
		this.setTooltip('');
		this.setHelpUrl('');
	},
	onchange: function(ev) {
		if(ev.type == Blockly.Events.UI && ev.element == 'click' && this.id == ev.blockId) {
			var block = this;
			var dialog = new goog.ui.Dialog();
			dialog.setContent(
				'<div id="futitle" style="position: absolute;top: 7%;left:0%;display: none;">等待秒数<span id="dian2" style="color:#00FFFF;">1</span></div>'+
				
				'<div id="dian" style="position:absolute;left: 24%;top: 13%;color:#00FFFF;font-size: 50px;width: 50%;text-align: center;">1</div>'+
				'<div id ="numdian" style="display:block; width: 70%; height: 70%; position: absolute; left: 24%; top: 14%; background: transparent;">' +
				'<div style="font-size: 32px;position: absolute; left: 13%; top: 20%; width: 15%; height: 15%;text-align: center;">1</div>' +
				'<div style="font-size: 32px;position: absolute; left: 30%; top: 20%; width: 15%; height: 15%;text-align: center;">2</div>' +
				'<div style="font-size: 32px;position: absolute; left: 47%; top: 20%; width: 15%; height: 15%;text-align: center;">3</div>' +
				'<div style="font-size: 32px;position: absolute; left: 13%; top: 40%; width: 15%; height: 15%;text-align: center;">4</div>' +
				'<div style="font-size: 32px;position: absolute; left: 30%; top: 40%; width: 15%; height: 15%;text-align: center;">5</div>' +
				'<div style="font-size: 32px;position: absolute; left: 47%; top: 40%; width: 15%; height: 15%;text-align: center;">6</div>' +
				'<div style="font-size: 32px;position: absolute; left: 13%; top: 60%; width: 15%; height: 15%;text-align: center;">7</div>' +
				'<div style="font-size: 32px;position: absolute; left: 30%; top: 60%; width: 15%; height: 15%;text-align: center;">8</div>' +
				'<div style="font-size: 32px;position: absolute; left: 47%; top: 60%; width: 15%; height: 15%;text-align: center;">9</div>' +
				'<div style="font-size: 32px;position: absolute; left: 13%; top: 80%; width: 15%; height: 15%;text-align: center;">0</div>' +
				'<div style="font-size: 32px;position: absolute; left: 30%; top: 80%; width: 15%; height: 15%;text-align: center;">.</div>' +
				'</div>' +
				'<div style="font-size: 22px;position: absolute; left:58%; top: 71%; width: 9%; text-align: center;color:#fff;" id="cancellast">删除</div>' +
				'<div id="cancelsruebut"><span id="cancelbut">取消</span><span id="sruebut">确定</span></div>'
			);
			dialog.setHasTitleCloseButton(false);
			dialog.setDraggable(false);
			dialog.setVisible(true);
			dialog.setDisposeOnHide(true);
			//保存当前的点位的值
			var b = block.getFieldValue('wait1');
			//设置点位
			var numdian = document.getElementById('numdian').getElementsByTagName('div');
			var dian = document.getElementById('dian');
			dian.innerHTML = block.getFieldValue('wait1');
			block.setFieldValue("", 'wait1');
			for(var i = 0; i < numdian.length; i++) {
				numdian[i].index = i;
				numdian[i].addEventListener('click', function() {
					var a = block.getFieldValue('wait1');
					dian.innerHTML = block.getFieldValue('wait1');
					if(a.length<5){
						block.setFieldValue(a + numdian_sec_string[this.index], 'wait1');
					}
					if(a.length<6&&dian.innerHTML>=10){
						block.setFieldValue(a + numdian_sec_string[this.index], 'wait1');
					}
					dian.innerHTML = block.getFieldValue('wait1');
					$('#numdian div').eq(this.index).css('color','#00C5AC').siblings().css('color','#fff');
				});
			}
			$("#cancellast").on('click',function(){
				block.setFieldValue('', 'wait1');
				dian.innerHTML = '';
			});
			//取消设定的值，还原到原来的值
			$("#cancelbut").on('click',function(){
				block.setFieldValue(b, 'wait1');
				$(".modal-dialog-bg").remove();
				$(".modal-dialog").remove();
			});
			//确定
			$("#sruebut").on('click',function(){
				$(".modal-dialog-bg").remove();
				$(".modal-dialog").remove();
			});

		}
	}
};
Blockly.JavaScript['gao_sleep'] = function(block) {
	var b = block.getFieldValue('wait1');
	var code = 'sleep ' + b + '\n';
	return code;
};


//数字
Blockly.Blocks['gao_number'] = {
	init: function() {
		this.appendDummyInput()
			.appendField(new TCR.FieldTextInput("1"), "n1");
		this.setInputsInline(true);
		this.setOutput(true, null);
		this.setColour(180);
		this.setTooltip('');
		this.setHelpUrl('');
	},
	onchange: function(ev) {
		if(ev.type == Blockly.Events.UI && ev.element == 'click' && this.id == ev.blockId) {
			var block = this;
			var dialog = new goog.ui.Dialog();
			dialog.setContent(
				'<div id="futitle" style="position: absolute;top: 7%;left:0%;display: none;">数<span id="dian2" style="color:#00FFFF;">1</span></div>'+
				
				'<div id="dian" style="position:absolute;left: 24%;top: 13%;color:#00FFFF;font-size: 50px;width: 50%;text-align: center;">1</div>'+
				'<div id ="numdian" style="display:block; width: 70%; height: 70%; position: absolute; left: 24%; top: 14%; background: transparent;">' +
				'<div style="font-size: 32px;position: absolute; left: 13%; top: 20%; width: 15%; height: 15%;text-align: center;">1</div>' +
				'<div style="font-size: 32px;position: absolute; left: 30%; top: 20%; width: 15%; height: 15%;text-align: center;">2</div>' +
				'<div style="font-size: 32px;position: absolute; left: 47%; top: 20%; width: 15%; height: 15%;text-align: center;">3</div>' +
				'<div style="font-size: 32px;position: absolute; left: 13%; top: 40%; width: 15%; height: 15%;text-align: center;">4</div>' +
				'<div style="font-size: 32px;position: absolute; left: 30%; top: 40%; width: 15%; height: 15%;text-align: center;">5</div>' +
				'<div style="font-size: 32px;position: absolute; left: 47%; top: 40%; width: 15%; height: 15%;text-align: center;">6</div>' +
				'<div style="font-size: 32px;position: absolute; left: 13%; top: 60%; width: 15%; height: 15%;text-align: center;">7</div>' +
				'<div style="font-size: 32px;position: absolute; left: 30%; top: 60%; width: 15%; height: 15%;text-align: center;">8</div>' +
				'<div style="font-size: 32px;position: absolute; left: 47%; top: 60%; width: 15%; height: 15%;text-align: center;">9</div>' +
				'<div style="font-size: 32px;position: absolute; left: 13%; top: 80%; width: 15%; height: 15%;text-align: center;">0</div>' +
				'<div style="font-size: 32px;position: absolute; left: 30%; top: 80%; width: 15%; height: 15%;text-align: center;">.</div>' +
				'</div>' +
				'<div style="font-size: 22px;position: absolute; left:58%; top: 71%; width: 9%; text-align: center;color:#fff;" id="cancellast">删除</div>' +
				'<div id="cancelsruebut"><span id="cancelbut">取消</span><span id="sruebut">确定</span></div>'
			);
			dialog.setHasTitleCloseButton(false);
			dialog.setDraggable(false);
			dialog.setVisible(true);
			dialog.setDisposeOnHide(true);
			//保存当前的点位的值
			var b = block.getFieldValue('n1');
			//设置点位
			var numdian = document.getElementById('numdian').getElementsByTagName('div');
			var dian = document.getElementById('dian');
			dian.innerHTML = block.getFieldValue('n1');
			block.setFieldValue("", 'n1');
			for(var i = 0; i < numdian.length; i++) {
				numdian[i].index = i;
				numdian[i].addEventListener('click', function() {
					var a = block.getFieldValue('n1');
					dian.innerHTML = block.getFieldValue('n1');
					if(a.length<5){
						block.setFieldValue(a + numdian_sec_string[this.index], 'n1');
					}
					if(a.length<6&&dian.innerHTML>=10){
						block.setFieldValue(a + numdian_sec_string[this.index], 'n1');
					}
					dian.innerHTML = block.getFieldValue('n1');
					$('#numdian div').eq(this.index).css('color','#00C5AC').siblings().css('color','#fff');
				});
			}
			$("#cancellast").on('click',function(){
				block.setFieldValue('', 'n1');
				dian.innerHTML = '';
			});
			//取消设定的值，还原到原来的值
			$("#cancelbut").on('click',function(){
				block.setFieldValue(b, 'n1');
				$(".modal-dialog-bg").remove();
				$(".modal-dialog").remove();
			});
			//确定
			$("#sruebut").on('click',function(){
				$(".modal-dialog-bg").remove();
				$(".modal-dialog").remove();
			});

		}
	}
};
Blockly.JavaScript['gao_number'] = function(block) {
	var b = block.getFieldValue('n1');
	var code = b
	return [code, Blockly.JavaScript.ORDER_ATOMIC];
};
